package ge.telasi.tasks.ui;

import ge.telasi.tasks.LocaleManager;
import ge.telasi.tasks.ejb.TaskFacadeRemote;
import ge.telasi.tasks.model.User;
import ge.telasi.tasks.ui.user.ServerItem;
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dimitri
 */
public class Platform {

    // versions
    public static final String VERSION_1_0_0 = "1.0.0_10062009";
    public static final String VERSION_1_0_1 = "1.0.1_12062009";
    public static final String VERSION_1_0_2 = "1.0.2_17062009";
    public static final String VERSION_1_0_3 = "1.0.3_24062009";
    public static final String VERSION_1_0_4 = "1.0.4_25062009";
    public static final String VERSION_1_0_5 = "1.0.5_01022010";
    public static final String VERSION_1_0_6 = "1.0.6_06042010";
    public static final String VERSION_1_0_7 = "1.0.7_17012011";

    // current version
    public static final String CURR_VERSION = VERSION_1_0_7;

    // program name
    public static final String NAME = "<html><font color='purple'>დაავალე</font>" +
            "<font color='red'>!</font></html>";

    // copyright
    public static final String COPY = "<html>&copy; 2009-2011, სს თელასი</html>";

    // current instance
    static Platform platform;

    public static Platform getDefault() {
        if (platform == null) {
            startPlatform();
        }
        return platform;
    }

    private static void startPlatform() {
        platform = new Platform();
    }

    // -- Get temporary directory

    public void writeAndOpenTempFile(String name, byte[] data) throws Exception {
        java.io.File dir = getTempDir();
        java.io.File f = new java.io.File(dir, System.currentTimeMillis() + name);
        f.createNewFile();
        OutputStream out = new FileOutputStream(f);
        out.write(data);
        out.flush();
        out.close();
        Desktop.getDesktop().open(f);
    }

    protected File getTempDir() {
        File f = new File(System.getProperty("user.home"), ".tasks/temp");
        f.mkdirs();
        return f;
    }

    // -- Server management

    private ServerManager serverManager = new ServerManager();

    public ServerManager getServerManager() {
        return serverManager;
    }
    
    public LocaleManager getLocaleManager() {
        return new UILocaleManager();
    }

    /**
     * Create default deployment platform.
     */
    public TaskFacadeRemote getFacade() throws Exception {
        return serverManager.getRemoteFacade();
    }

    public Map getCredentials() {
        return getCredentials(serverManager.getPassword());
    }

    public Map getCredentials(String password) {
        Map credentials = new HashMap();
        if (serverManager.getUser() != null) {
            credentials.put("username", serverManager.getUser().getUsername());
        }
        if (password != null) {
            credentials.put("password", password);
        }
        credentials.put("version", CURR_VERSION);
        credentials.put("client", "swing");
        return credentials;
    }

    public boolean isConnected() {
        return serverManager.isConnected();
    }

    public ServerItem getServerItem() {
        return serverManager.getServerItem();
    }

    public void setServerItem(ServerItem serverItem) {
        serverManager.setServerItem(serverItem);
    }

    public void changeUser(User user) {
        serverManager.setUser(user);
    }

    public void changePassword(String password) {
        serverManager.setPassword(password);
    }

    public void disconnect() {
        serverManager.disconnect();
    }

    public User getUser() {
        return serverManager.getUser();
    }

    public void login(ServerItem item, String username, String password, boolean savePassword) throws Exception {
        serverManager.login(this, item, username, password, savePassword);
    }

    // -- Main frame operations

    public boolean validateConnection() {
        return Launcher.validateConnection();
    }

    public void resetOnConnectionChange() {
        Launcher.resetMainWindowTitle();
        Launcher.validateTrayIcon();
    }

    public void showMainWindow() {
        Launcher.showMainFrame();
    }

    public void hideMainWindow() {
        Launcher.hideMainFrame();
    }

    // -- Notification operations

    public void addNotification(String message) {
        Launcher.addNotification(message);
    }

    public void refreshTaskList() {
        Launcher.refreshTaskList();
    }
}
